package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.CapsStyle;
	import flash.display.LineScaleMode;
	import flash.geom.Matrix;
	/**
	 * @author perry
	 */
	public class UTILS
	{
		public static function strReplace( input:String, replace:String, replaceWith:String ):String
		{
		    //change to StringBuilder
		    var sb:String = new String();
		    var found:Boolean = false;
		 
		    var sLen:Number = input.length;
		    var rLen:Number = replace.length;
		 
		    for (var i:Number = 0; i < sLen; i++)
		    {
		        if(input.charAt(i) == replace.charAt(0))
		        {
		            found = true;
		            for(var j:Number = 0; j < rLen; j++)
		            {
		                if(!(input.charAt(i + j) == replace.charAt(j)))
		                {
		                    found = false;
		                    break;
		                }
		            }
		 
		            if(found)
		            {
		                sb += replaceWith;
		                i = i + (rLen - 1);
		                continue;
		            }
		        }
		        sb += input.charAt(i);
		    }
		    //TODO : if the string is not found, should we return the original
		    //string?
		    return sb;
		}
		
		public static function polarGradient(mc:Object, radius:Number):void
		{
//		    var shape:Shape = new Shape();
		    var count:uint = 1080;
		    var degreesToRadians:Number = Math.PI / 180;
		   
		    for(var i:int = 0; i < count; i++)
		    {
		        var b:uint = (i / count) * 0xFF;
		        var g:uint = b << 8;
		        var r:uint = b << 16;
		        var color:uint = r | g | b;
		        var angle:Number = i / (count / 360) * degreesToRadians;
		       
//		        mc.graphics.clear();
		        mc.graphics.lineStyle(1, color, 1, false, LineScaleMode.NONE, CapsStyle.NONE);
		        mc.graphics.moveTo(0, 0);
		        mc.graphics.lineTo(Math.cos(angle) * radius, Math.sin(angle) * radius);
		    }
		   
//		    return shape;
		}
		
		public static function drawScaled( obj:BitmapData, thumbWidth:Number, thumbHeight:Number ):Bitmap
		{
		    var m:Matrix = new Matrix();
		    m.scale(thumbWidth / obj.width, thumbHeight / obj.height);
		    
		    var bmp:BitmapData = new BitmapData(thumbWidth, thumbHeight, false);
		    bmp.draw(obj, m);
		    
		    return new Bitmap(bmp);
		}
	}
}
